<script setup lang="ts">
import router from '@/router'
import { ref } from 'vue'
import { userLoginService, userRegisterService } from '@/api/user'
import { useUserStore } from '@/stores/user'

const login = () => {
  router.push('/login')
}

const form = ref({
  account: '',
  password: '',
  confirmPassword: '',
  identify: 'student'
})

const register = ref()

const validateConfirmPassword = (rule: any, value: string, callback: Function) => {
  if (value !== form.value.password) {
    callback(new Error('两次输入的密码不一致!'))
  } else {
    callback()
  }
}

const rule = {
  account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, max: 20, message: '长度在 8 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请输入确认密码', trigger: 'blur' },
    { min: 8, max: 20, message: '长度在 8 到 20 个字符', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

const useStore = useUserStore()
const submitForm = async () => {
  const isValid = await register.value.validate()
  if (isValid) {
    await userRegisterService(form.value)
    const res = await userLoginService({
      account: form.value.account,
      password: form.value.password,
      identify: 'student'
    })
    useStore.token = res.data.data.token
    useStore.setIdentify(res.data.data.identify)
    useStore.userId = res.data.data.id
    await ElMessage.success('注册成功')
    setTimeout(() => router.push('/student'), 500)
  }
}
</script>

<template>
  <div id="main-register">
    <div class="register">
      <h1 style="text-align: center">学生注册</h1>
      <el-form ref="register" :model="form" :rules="rule" style="margin-top: 20px">
        <el-form-item prop="account">
          <el-input class="input" placeholder="请输入账号" v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            class="input"
            type="password"
            show-password
            placeholder="密码应是8-20位，包含字母和数字"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            class="input"
            type="password"
            show-password
            placeholder="请确认密码"
            v-model="form.confirmPassword"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" class="button" @click="submitForm">注册</el-button>
      <div style="margin-top: 20px; width: 300px; display: flex; justify-content: end">
        <el-link @click="login">返回登录</el-link>
      </div>
    </div>
  </div>
</template>

<style scoped>
#main-register {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/image/avatar/background.jpg');
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register {
  width: 400px;
  height: 400px;
  background-color: whitesmoke;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  padding: 20px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.input {
  width: 300px;
  height: 50px;
}

.button {
  width: 300px;
  height: 50px;
}
</style>
